using micromsg;
using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using System;
using System.Collections.Generic;
namespace MicroMsg.Scene
{
	public class NetSceneBatchGetHeadImg : NetSceneBaseEx<BatchGetHeadImgRequest, BatchGetHeadImgResponse, BatchGetHeadImgRequest.Builder>
	{
		private const string TAG = "NetSceneBatchGetHeadImg";
		private const int MAX_COUNT = 10;
		private int mCurrentCount;
		public List<string> mUserNameListGetting = new List<string>();
		public bool isGetting;
		public bool doScene(List<string> userNameList)
		{
			using (List<string>.Enumerator enumerator = userNameList.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					string current = enumerator.get_Current();
					if (!this.mUserNameListGetting.Contains(current))
					{
						this.mUserNameListGetting.Add(current);
					}
				}
			}
			if (this.isGetting)
			{
				Log.smethod_5("NetSceneBatchGetHeadImg", "is getting headimg now");
				return true;
			}
			if (this.mUserNameListGetting.get_Count() == 0)
			{
				Log.smethod_5("NetSceneBatchGetHeadImg", "no user need get");
				return true;
			}
			this.doScene();
			return true;
		}
		private bool doScene()
		{
			Log.smethod_5("NetSceneBatchGetHeadImg", "BatchGetHeadImgRequest begin  num = " + this.mUserNameListGetting.get_Count());
			this.isGetting = true;
			base.beginBuilder();
			this.mBuilder.BaseRequest = NetSceneBase.makeBaseRequest(15);
			using (List<string>.Enumerator enumerator = this.mUserNameListGetting.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					string current = enumerator.get_Current();
					this.mBuilder.UserNameListList.Add(Util.toSKString(current));
				}
			}
			this.mBuilder.Count = (uint)this.mBuilder.UserNameListList.get_Count();
			this.mSessionPack.mConnectMode = 2;
			this.mSessionPack.mCmdID = 15;
			base.endBuilder();
			return true;
		}
		public void resetFlag()
		{
			this.mCurrentCount = 0;
			this.isGetting = false;
			this.mUserNameListGetting.Clear();
		}
		protected override void onFailed(BatchGetHeadImgRequest request, BatchGetHeadImgResponse response)
		{
			Log.smethod_1("NetSceneBatchGetHeadImg", "get head img failed,net work err");
			List<string> list = new List<string>();
			list.AddRange(this.mUserNameListGetting);
			EventCenter.postEvent(EventConst.ON_NETSCENE_BATCHGETHEADIMG_ERR, list, null);
			this.resetFlag();
		}
		protected override void onSuccess(BatchGetHeadImgRequest request, BatchGetHeadImgResponse response)
		{
			if (response.BaseResponse.Ret == 0 && response.ImgPairListList.get_Count() > 0)
			{
				this.saveInfo(request, response);
				return;
			}
			Log.smethod_1("NetSceneBatchGetHeadImg", string.Concat(new object[]
			{
				"get head img failed,response.BaseResponse.Ret = ",
				response.BaseResponse.Ret,
				"response.ImgPairListList.Count",
				response.ImgPairListList.get_Count()
			}));
			List<string> list = new List<string>();
			list.AddRange(this.mUserNameListGetting);
			EventCenter.postEvent(EventConst.ON_NETSCENE_BATCHGETHEADIMG_ERR, list, null);
			this.resetFlag();
		}
		private bool saveInfo(BatchGetHeadImgRequest request, BatchGetHeadImgResponse response)
		{
			using (IEnumerator<ImgPair> enumerator = response.ImgPairListList.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					ImgPair current = enumerator.get_Current();
					HeadImageMgr.save(current.Username.String, current.ImgBuf.Buffer.ToByteArray());
					this.mUserNameListGetting.Remove(current.Username.String);
					this.mCurrentCount = 0;
				}
			}
			this.mCurrentCount++;
			if (this.mUserNameListGetting.get_Count() > 0 && this.mCurrentCount > 10)
			{
				Log.smethod_1("NetSceneBatchGetHeadImg", "error to BatchGetHeadImgRequest,too many headimg to get");
				List<string> list = new List<string>();
				list.AddRange(this.mUserNameListGetting);
				EventCenter.postEvent(EventConst.ON_NETSCENE_BATCHGETHEADIMG_ERR, list, null);
				this.resetFlag();
				return false;
			}
			if (this.mUserNameListGetting.get_Count() > 0 && this.mCurrentCount <= 10)
			{
				Log.smethod_5("NetSceneBatchGetHeadImg", "continue to BatchGetHeadImgRequest");
				this.doScene();
				return true;
			}
			Log.smethod_5("NetSceneBatchGetHeadImg", "finished to BatchGetHeadImgRequest");
			this.resetFlag();
			return true;
		}
	}
}
